package com.example.lanouhn.bantangapp.activities.fragments;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.activities.ActivityLand;
import com.example.lanouhn.bantangapp.activities.activities.AvatarArticle;
import com.example.lanouhn.bantangapp.activities.activities.AvatarDetailedlistActivity;
import com.example.lanouhn.bantangapp.activities.activities.AvatarFansActivity;
import com.example.lanouhn.bantangapp.activities.activities.AvatarFollowActivity;
import com.example.lanouhn.bantangapp.activities.activities.AvatarOrderActivity;
import com.example.lanouhn.bantangapp.activities.activities.AvatarSetupActivity;
import com.example.lanouhn.bantangapp.activities.activities.AvatarThesunActivity;
import com.example.lanouhn.bantangapp.activities.activities.Avatar_SingleProduct;
import com.example.lanouhn.bantangapp.activities.activities.ShowDialog_Avatar;
import com.example.lanouhn.bantangapp.activities.contants.LoginNP;
import com.example.lanouhn.bantangapp.activities.contants.LoginNP_url;
import com.example.lanouhn.bantangapp.activities.contants.LoginSet;
import com.example.lanouhn.bantangapp.activities.contants.MyEvent;
import com.example.lanouhn.bantangapp.activities.contants.Search_hottag_url;
import com.example.lanouhn.bantangapp.activities.utils.HttpUtils;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_LoginNP;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_LoginSet;
import com.example.lanouhn.bantangapp.activities.utils.SPUtils;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/7/22.
 */
public class Fragment_Avatar extends Fragment {
    private ImageView image_order;
    private ImageView avatar;//用户头像
    private LinearLayout follow;//关注
    private LinearLayout fans;  //粉丝
    private LinearLayout detailedlist;//清单
    private LinearLayout thesun;//晒单
    private ImageView    iv_setup;//设置
    private LinearLayout ll_photo;//头像区域弹出框
    private LinearLayout ll_article;//文章
    private TextView   name;//昵称
    private TextView   gxname;//个性签名
    private LinearLayout ll_Single_product;//单品


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.avatar_fragment,container,false);
        image_order= (ImageView) view.findViewById(R.id.image_order);
        avatar= (ImageView) view.findViewById(R.id.avatar);

        follow= (LinearLayout) view.findViewById(R.id.follow);
        fans= (LinearLayout) view.findViewById(R.id.fans);
        detailedlist= (LinearLayout) view.findViewById(R.id.detailedlist);
        thesun= (LinearLayout) view.findViewById(R.id.thesun);
        iv_setup= (ImageView) view.findViewById(R.id.iv_setup);
        ll_photo= (LinearLayout) view.findViewById(R.id.ll_photo);
        ll_article= (LinearLayout) view.findViewById(R.id.ll_article);
        name= (TextView) view.findViewById(R.id.name);

        //设置昵称
        name.setText(SPUtils.get(getActivity(),"nickname","").toString());

        gxname= (TextView) view.findViewById(R.id.gxname);
        ll_Single_product= (LinearLayout) view.findViewById(R.id.ll_Single_product);

        //注册eventbus   register(this)  this 是接收的对象
        EventBus.getDefault().register(Fragment_Avatar.this);
        initLinstener();
        return view;
    }


    /**
     * 接收事件的方法
     *  @Subscribe  这个一定要写!!!!!  ThreadMode.MAIN 是在主线程  BACKGROUD是后台操作
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void initEvent(MyEvent event){
        //name.setText(event.getMsg());
        gxname.setText(event.getMsg1());
    }


    //监听点击事件
    private void initLinstener() {
        //跳转订单
        image_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(),AvatarOrderActivity.class);
                startActivity(it);
            }
        });
        //跳转关注
        follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(),AvatarFollowActivity.class);
                startActivity(it);
            }
        });
        //跳转粉丝
        fans.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(),AvatarFansActivity.class);
                startActivity(it);
            }
        });
        //跳转清单
        detailedlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(),AvatarDetailedlistActivity.class);
                startActivity(it);
            }
        });
        //跳转晒单
        thesun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(),AvatarThesunActivity.class);
                startActivity(it);
            }
        });
        //跳转设置
        iv_setup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(),AvatarSetupActivity.class);
                startActivity(it);
            }
        });
        //弹出框
        ll_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(), ShowDialog_Avatar.class);
                startActivity(it);
            }
        });
        //文章页面跳转
        ll_article.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(), AvatarArticle.class);
                startActivity(it);
            }
        });
        //跳转 单品
        ll_Single_product.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(), Avatar_SingleProduct.class);
                startActivity(it);
            }
        });

    }
    //post解析数据
//    private void initDataToPost() {
//
//        list = new ArrayList<>();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//
//                String url = LoginNP_url.setupurl;//获取网络地址
//                String params="app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id="+key+"&oauth_token=b149f3a97e42c22a0f63253cfe190c99&track_deviceid=868596020655823&track_device_info=m2+note&channel_name=UMENG_CHANNEL&app_installtime=1468811189&app_versions=5.8.4&os_versions=5.1&screensize=1080&v=16";
//                String result = HttpUtils.doPost(url,params);
//
//                if (null!=result&&result.length()>0){
//                    list = JsonUtils_LoginSet.getlloginList(result);
//                    handler.sendEmptyMessage(1);
//                }else{
//                    handler.sendEmptyMessage(0);
//                }
//
//            }
//
//
//        }).start();
//    }
//
//
//    private Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what) {
//                case 1:
//                    LoginSet l = new LoginSet();
//                    Picasso.with(getContext()).load(l.getAvatar()).into(avatar);
//                    break;
//                case 0:
//
//                    Intent it = new Intent(getActivity(), ActivityLand.class);
//                    startActivity(it);
//                    break;
//
//            }
//        }
//    };
}
